cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
SET(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

project("cae_data_convert")

if(MSVC)
    option(EXE_RELEASE_PDB "SET RELEASE GENERATE PDB FILE" ON)
    OPTION(EXE_RELEASE_SEH "Enable Structured Exception Handling for Release version." OFF)

    SET(CMAKE_CXX_FLAGS "/Zc:strictStrings")

    # 启用异常
    add_compile_options(/EHsc)
endif()

SET(CMAKE_CXX_FLAGS_RELEASE "-O2")

if(EXE_RELEASE_PDB)
    SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
    add_definitions(/Zi)
    add_definitions(/Od)
    SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG")
    SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG")
endif()

if(WIN32)
    add_definitions(-DWIN32)
endif()

set(WORK_ROOT_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(WORK_DIR ${WORK_ROOT_DIR}/Release/)
file(MAKE_DIRECTORY ${WORK_DIR}) #创建文件夹

# VTK
option(USE_SDK_VTK_SYSTEM "Use vtk sdk of system." OFF)
if(NOT USE_SDK_VTK_SYSTEM)
    if(WIN32)
        set(VTK_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Windows/VTK)
        set(VTK_DIR ${VTK_ROOT_DIR}/lib/cmake/vtk-9.3)
        file(GLOB VTK_DLL ${VTK_ROOT_DIR}/bin/*.dll )
        file(COPY ${VTK_DLL} DESTINATION ${WORK_DIR})
    elseif(UNIX)
        set(VTK_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Linux/VTK)
        set(VTK_DIR ${VTK_ROOT_DIR}/lib64/cmake/vtk-9.3)
        file(GLOB VTK_LIBS ${VTK_ROOT_DIR}/lib64/*.so ${VTK_ROOT_DIR}/lib64/*.so.*)
        file(COPY ${VTK_LIBS} DESTINATION ${WORK_DIR})
    endif()
endif()
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

# Python
if(WIN32)
    set(PYTHON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Windows/Python)
    set(PYTHON_INCLUDE_DIR ${PYTHON_DIR}/include)
    set(PYTHON_NUMPY_DIR ${PYTHON_DIR}/Lib/site-packages/numpy)
    set(PYTHON_NUMPY_INCLUDE_DIR ${PYTHON_NUMPY_DIR}/core/include/numpy)
    include_directories(${PYTHON_NUMPY_INCLUDE_DIR})
    file(GLOB PYTHON_LIBS ${PYTHON_DIR}/libs/*.lib )
    file(GLOB PYTHON_DLLS ${PYTHON_DIR}/*.dll )
    file(GLOB PYTHON_NUMPY_LIBS ${PYTHON_NUMPY_DIR}/core/lib/*.lib)
    set(PYTHON_LIB ${PYTHON_DIR}/Lib)
    set(PYTHON_DLL ${PYTHON_DIR}/DLLs)
    file(COPY ${PYTHON_DLL} DESTINATION ${WORK_DIR})
    file(COPY ${PYTHON_DLLS} DESTINATION ${WORK_DIR})
else()
    set(PYTHON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Linux/Python)
    set(PYTHON_INCLUDE_DIR ${PYTHON_DIR}/include/python3.12)
    set(PYTHON_LIB ${PYTHON_DIR}/lib)
    file(GLOB PYTHON_LIBS ${PYTHON_LIB}/*.so ${PYTHON_LIB}/*.so.*)
    file(COPY ${PYTHON_LIBS} DESTINATION ${WORK_DIR})
endif()
include_directories(${PYTHON_INCLUDE_DIR})
file(COPY ${PYTHON_LIB} DESTINATION ${WORK_DIR})

# rst/odb is enable
if(ENABLE_RST OR ENABLE_ODB)
    if(WIN32)
        set(RSTODBSDK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/SDK/Windows/RstOdbSDK)
        set(RSTODBSDK_BIN_DIR ${RSTODBSDK_DIR}/bin)
        set(RSTODBSDK_LIB_DIR ${RSTODBSDK_DIR}/lib)
        file(GLOB RSTODBSDK_LIB ${RSTODBSDK_LIB_DIR}/IBE_Common.lib ${RSTODBSDK_LIB_DIR}/IBE_VTK.lib)
        file(GLOB RSTODBSDK_DLL ${RSTODBSDK_BIN_DIR}/IBE_Common.dll ${RSTODBSDK_BIN_DIR}/IBE_VTK.dll)
        file(COPY ${RSTODBSDK_DLL} DESTINATION ${WORK_DIR})
    else()
        set(RSTODBSDK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/SDK/Linux/RstOdbSDK)
        set(RSTODBSDK_LIB_DIR ${RSTODBSDK_DIR}/lib)
        file(GLOB RSTODBSDK_LIB ${RSTODBSDK_LIB_DIR}/libIBE_Common.so ${RSTODBSDK_LIB_DIR}/libIBE_VTK.so)
        file(COPY ${RSTODBSDK_LIB} DESTINATION ${WORK_DIR})
    endif()
    set(RSTODBSDK_INCLUDE_DIR ${RSTODBSDK_DIR}/include)
    set(IBE_COMMON_HEADER_DIR ${RSTODBSDK_INCLUDE_DIR}/IBE_Common)
    set(IBE_VTK_HEADER_DIR ${RSTODBSDK_INCLUDE_DIR}/IBE_VTK)
    file(GLOB IBE_COMMON_H ${IBE_Common_HEADER_DIR}/*.h)
    file(GLOB IBE_VTK_H ${IBE_VTK_HEADER_DIR}/*.h)

    include_directories(${IBE_COMMON_HEADER_DIR})
    include_directories(${IBE_VTK_HEADER_DIR})
endif()

# rst
option(ENABLE_RST "Enable of rst." OFF)
if(ENABLE_RST)
    add_definitions(-DENABLE_RST)
    if(WIN32)
        file(GLOB RST_LIB ${RSTODBSDK_LIB_DIR}/IBE_Ansys.lib)
        file(GLOB RST_DLL ${RSTODBSDK_BIN_DIR}/IBE_Ansys.dll ${RSTODBSDK_BIN_DIR}/IBE_AnsysRstReader.pyd)
        file(COPY ${RST_DLL} DESTINATION ${WORK_DIR})
    else()
        file(GLOB RST_LIB ${RSTODBSDK_LIB_DIR}/libIBE_Ansys.so)
        file(GLOB RST_PYTHON_LIB ${RSTODBSDK_LIB_DIR}/IBE_AnsysRstReader.so)
        file(COPY ${RST_LIB} DESTINATION ${WORK_DIR})
        file(COPY ${RST_PYTHON_LIB} DESTINATION ${WORK_DIR})
    endif()

    set(IBE_ANSYS_HEADER_DIR ${RSTODBSDK_INCLUDE_DIR}/IBE_Ansys)
    file(GLOB IBE_ANSYS_H ${IBE_ANSYS_HEADER_DIR}/*.h)

    include_directories(${IBE_ANSYS_HEADER_DIR})
    include_directories(${CMAKE_SOURCE_DIR}/parserst)
    add_subdirectory("parserst")
endif()

# odb
option(ENABLE_ODB "Enable of odb." OFF)
if(ENABLE_ODB)
    add_definitions(-DENABLE_ODB)
    if(WIN32)
        add_definitions(-D_WINDOWS_SOURCE)
        set(ABAQUS_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Windows/Abaqus)
        set(ABAQUS_DIR ${ABAQUS_ROOT_DIR}/2022)
        include_directories(${ABAQUS_DIR}/)
        include_directories(${ABAQUS_DIR}/win_b64/code/include/)
        file(GLOB ABAQUS_LIB ${ABAQUS_DIR}/win_b64/code/lib/*.lib )
        file(GLOB ABAQUS_DLL ${ABAQUS_DIR}/win_b64/code/bin/*.dll )
        file(COPY ${ABAQUS_LIB} DESTINATION ${WORK_DIR})
        file(COPY ${ABAQUS_DLL} DESTINATION ${WORK_DIR})

        file(GLOB ODB_LIB ${RSTODBSDK_LIB_DIR}/IBE_Abaqus.lib)
        file(GLOB ODB_DLL ${RSTODBSDK_BIN_DIR}/IBE_Abaqus.dll)
        file(COPY ${ODB_DLL} DESTINATION ${WORK_DIR})
    elseif(UNIX)
        add_definitions(-D_LINUX_SOURCE)
        set(ABAQUS_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Linux/Abaqus)
        set(ABAQUS_DIR ${ABAQUS_ROOT_DIR}/2022)
        include_directories(${ABAQUS_DIR}/)
        include_directories(${ABAQUS_DIR}/linux_a64/code/include/)
        file(GLOB ABAQUS_LIB ${ABAQUS_DIR}/linux_a64/code/bin/*.so ${ABAQUS_DIR}/linux_a64/code/bin/*.so.*)
        file(COPY ${ABAQUS_LIB} DESTINATION ${WORK_DIR})

        file(GLOB ODB_LIB ${RSTODBSDK_LIB_DIR}/libIBE_Abaqus.so)
        file(COPY ${ODB_LIB} DESTINATION ${WORK_DIR})
    endif()

    set(IBE_ABAQUS_HEADER_DIR ${RSTODBSDK_INCLUDE_DIR}/IBE_Abaqus)
    file(GLOB IBE_ABAQUS_H ${IBE_ABAQUS_HEADER_DIR}/*.h)

    add_definitions(-DIBE_ABAQUS_EXPORTS)

    include_directories(${IBE_ABAQUS_HEADER_DIR})
    include_directories(${CMAKE_SOURCE_DIR}/parseodb)
    add_subdirectory("parseodb")
endif()

if(WIN32)
    # System
    set(SYSTEM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Windows/System)
    file(GLOB SYSTEM_DLL ${SYSTEM_DIR}/*.dll )
    file(COPY ${SYSTEM_DLL} DESTINATION ${WORK_DIR})
endif()

# cgns
option(ENABLE_CGNS "Enable of cgns." OFF)
if(ENABLE_CGNS)
    add_definitions(-DENABLE_CGNS)

    include_directories(${CMAKE_SOURCE_DIR}/parsecgns)
    add_subdirectory("parsecgns")
endif()

include_directories(${CMAKE_SOURCE_DIR}/Main)
include_directories(${CMAKE_SOURCE_DIR}/parsebdf)
include_directories(${CMAKE_SOURCE_DIR}/parsecdb)
include_directories(${CMAKE_SOURCE_DIR}/parsecdb)
include_directories(${CMAKE_SOURCE_DIR}/parseinp)
include_directories(${CMAKE_SOURCE_DIR}/parseop2)
include_directories(${CMAKE_SOURCE_DIR}/Split)

add_subdirectory("Main")
add_subdirectory("parsebdf")
add_subdirectory("parsecdb")
add_subdirectory("parseinp")
add_subdirectory("parseop2")
add_subdirectory("Split")
